
**************************
*** Summary Statistics ***
**************************

use "$altered_data/event_study_vaccination.dta", clear
keep if date >= td(13jun2021) & date <= td(04oct2021)

gen timing = treated + treated_c

forvalues x=0/3 {
	
	if `x' == 3 {
	local subsample "inlist(timing, 0, 1, 2)"
	}
	else {
		local subsample "inlist(timing, `x')"
	}
	
	summ cases_melde if age == 0 & `subsample'
	matrix m`x' = r(mean), r(sd)

	forvalues a = 1/4 {
		summ cases_melde if age == `a' & `subsample'
		matrix m`x' = m`x' \ r(mean), r(sd)
	}

	summ cases_melde_total if `subsample' 
	matrix m`x' = m`x' \ r(mean), r(sd)

}

matrix m = m3, m0, m1, m2


global f0 "%12.0fc"
global f3 "%4.1f"

cap drop table
gen table=""

local line " & \multicolumn{2}{c}{Full} & \multicolumn{2}{c}{Before} & \multicolumn{2}{c}{During} & \multicolumn{2}{c}{After} \\"
qui replace table="`line'" in 1

local line " & \multicolumn{2}{c}{Period} & \multicolumn{2}{c}{Summer Break} & \multicolumn{2}{c}{Summer Break} & \multicolumn{2}{c}{Summer Break} \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-9}"
qui replace table="`line'" in 2


local line " & Mean & SD & Mean & SD & Mean & SD & Mean & SD \\"
qui replace table="`line'" in 3

local line "\hline"
qui replace table="`line'" in 4

local line "\textit{Age Group} &  & & & & & & & \\"
qui replace table="`line'" in 5

forvalues s = 1/8 {
	local s`s' = m[2,`s'] 
}
local line "5-14 &" $f3 `s1' "&" $f3 `s2' "&" $f3 `s3' "&" $f3 `s4' "&" $f3 `s5' "&" $f3 `s6' "&" $f3 `s7' "&" $f3 `s8' "\\"
local line: display "`line'"
qui replace table="`line'" in 6

forvalues s = 1/8 {
	local s`s' = m[3,`s'] 
}
local line "15-34 &" $f3 `s1' "&" $f3 `s2' "&" $f3 `s3' "&" $f3 `s4' "&" $f3 `s5' "&" $f3 `s6' "&" $f3 `s7' "&" $f3 `s8' "\\"
local line: display "`line'"
qui replace table="`line'" in 7

forvalues s = 1/8 {
	local s`s' = m[4,`s'] 
}
local line "35-59 &" $f3 `s1' "&" $f3 `s2' "&" $f3 `s3' "&" $f3 `s4' "&" $f3 `s5' "&" $f3 `s6' "&" $f3 `s7' "&" $f3 `s8' "\\"
local line: display "`line'"
qui replace table="`line'" in 8

forvalues s = 1/8 {
	local s`s' = m[5,`s'] 
}
local line "60+ &" $f3 `s1' "&" $f3 `s2' "&" $f3 `s3' "&" $f3 `s4' "&" $f3 `s5' "&" $f3 `s6' "&" $f3 `s7' "&" $f3 `s8' "\\"
local line: display "`line'"
qui replace table="`line'" in 9

forvalues s = 1/8 {
	local s`s' = m[6,`s'] 
}
local line "All Ages &" $f3 `s1' "&" $f3 `s2' "&" $f3 `s3' "&" $f3 `s4' "&" $f3 `s5' "&" $f3 `s6' "&" $f3 `s7' "&" $f3 `s8' "\\"
local line: display "`line'"
qui replace table="`line'" in 12

local line "\hline"
local line: display "`line'"
qui replace table="`line'" in 13


quietly tab RS, nofreq
scalar N = r(r)

quietly tab date
scalar T = r(r)

local NT_full = N * T

count if timing == 0
local NT_timing0 = r(N) / 4

count if timing == 1
local NT_timing1 = r(N) / 4

count if timing == 1
local NT_timing2 = r(N) / 4

local line "Observations & \multicolumn{2}{p{1cm}}{" $f0 `NT_full' "}& \multicolumn{2}{p{1cm}}{"  $f0 `NT_timing0' "}& \multicolumn{2}{p{1cm}}{" $f0 `NT_timing1' "}& \multicolumn{2}{p{1cm}}{" $f0 `NT_timing2' "} \\"
local line: display "`line'"
qui replace table="`line'" in 13

local line "\hline\hline"
local line: display "`line'"
qui replace table="`line'" in 14

outfile table using "${tables}summary_stat.tex" in 1/14, replace noquote

//doublecheck results:
tab age, sum(cases_melde) 
su cases_melde_total

tab age if timing == 0, sum(cases_melde) 
su cases_melde_total if timing == 0

tab age if timing == 1, sum(cases_melde) 
su cases_melde_total if timing == 1

tab age if timing == 2, sum(cases_melde) 
su cases_melde_total if timing == 2




*************************************
*** Summary statistics by regions ***
*************************************


use "$altered_data/event_study_vaccination.dta", clear
//pop_total not completly right
replace cum_cases = cum_cases/pop_total 
collapse cases_melde deaths_agg cum_cases  treated (sum) pop_total, by(bundesland date age)

sort bundesland date age

reshape wide cases_melde, i(bundesland date) j(age)

bys bundesland treated: gen rel_date = _n
bys bundesland treated: egen max_rel_date = max(rel_date)
replace rel_date = max_rel_date - rel_date

//keep only one week prior to treatment for each region
keep if treated == 0
keep if rel_date <= 6

collapse (mean) cases_melde1 cases_melde2 cases_melde3 cases_melde4 deaths_agg pop_total, by(bundesland)

label var cases_melde1 "Cases 5 - 14"
label var cases_melde2 "Cases 15 - 34"
label var cases_melde3 "Cases 35 - 59"
label var cases_melde4 "Cases 60+"
label var deaths_agg "Deaths per 100k"

gen _date_treatment = ""
label var _date_treatment "First day of school"

replace _date_treatment = "Aug 1st" if bundesland == "Mecklenburg-Vorpommern"
replace _date_treatment = "Aug 1st" if bundesland == "Schleswig-Holstein"
replace _date_treatment = "Aug 4th" if bundesland == "Hamburg"
replace _date_treatment = "Aug 7th" if bundesland == "Berlin"
replace _date_treatment = "Aug 8th" if bundesland == "Brandenburg"
replace _date_treatment = "Aug 18th" if bundesland == "Nordrhein-Westfalen"
replace _date_treatment = "Aug 28th" if bundesland == "Hessen"
replace _date_treatment = "Aug 28th" if bundesland == "Rheinland-Pfalz"
replace _date_treatment = "Aug 28th" if bundesland == "Saarland"
replace _date_treatment = "Sep 2nd" if bundesland == "Bremen"
replace _date_treatment = "Sep 2nd" if bundesland == "Niedersachsen"
replace _date_treatment = "Sep 2nd" if bundesland == "Sachsen-Anhalt"
replace _date_treatment = "Sep 4th" if bundesland == "Sachsen"
replace _date_treatment = "Sep 5th" if bundesland == "Thüringen"
replace _date_treatment = "Sep 12th" if bundesland == "Baden-Württemberg"
replace _date_treatment = "Sep 14th" if bundesland == "Bayern"

gen _no_treated = .
replace _no_treated = 1 if bundesland == "Mecklenburg-Vorpommern"
replace _no_treated = 2 if bundesland == "Schleswig-Holstein"
replace _no_treated = 3 if bundesland == "Hamburg"
replace _no_treated = 4 if bundesland == "Berlin"
replace _no_treated = 5 if bundesland == "Brandenburg"
replace _no_treated = 6 if bundesland == "Nordrhein-Westfalen"
replace _no_treated = 7 if bundesland == "Hessen"
replace _no_treated = 8 if bundesland == "Rheinland-Pfalz"
replace _no_treated = 9 if bundesland == "Saarland"
replace _no_treated = 10 if bundesland == "Bremen"
replace _no_treated = 11 if bundesland == "Niedersachsen"
replace _no_treated = 12 if bundesland == "Sachsen-Anhalt"
replace _no_treated = 13 if bundesland == "Sachsen"
replace _no_treated = 14 if bundesland == "Thüringen"
replace _no_treated = 15 if bundesland == "Baden-Württemberg"
replace _no_treated = 16 if bundesland == "Bayern"

sort _no_treated
drop _no_treated

label var pop_total "Population (in 1m)"
replace pop_total = pop_total/1000000

foreach var of varlist cases_melde1 cases_melde2 cases_melde3 cases_melde4 deaths_agg  pop_total{
replace `var' = round(`var',.1)
}

order bundesland _date_treatment  cases_melde1 cases_melde2 cases_melde3 cases_melde4 pop_total

texsave bundesland _date_treatment  cases_melde1 cases_melde2 cases_melde3 cases_melde4 pop_total using "${tables}bystate.tex" , replace  frag varlabels






